package com.gxa.ehome.controller;


import com.gxa.ehome.busiCodeEnum.ErrorCode;
import com.gxa.ehome.exception.BusinessException;
import com.gxa.ehome.pojo.SysUser;

import com.gxa.ehome.service.SysUserService;
import com.gxa.ehome.util.MapUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

//类中和方法都制定了method,那么对于register方法 两种都有效
//建议不再类中声明method
//









@Controller
@RequestMapping(value = "/sysUser2"/*,method =RequestMethod.GET*/)
public class SysUserController2 {

//    @RequestMapping(value = "/reg",method = {RequestMethod.POST,RequestMethod.GET})


    //通过注解来确定请求类型 @GetMapping使用Get方式
    //当参数是对象类型是,前端通过传递对象属性值,使用@RequestBody
    //来完成对象数据的注入
    //前端传递的参数中 name要和目标对象的属性值匹配.
    //前端是使用表单或者ajax形式(vue/layui,以data形式添加参数,通过json传递给后端
    // 后端只能用post方式来接收,不能用get)
    //@GetMapping(value = "/reg")
    @PostMapping(value = "/reg")
    public void register(@RequestBody SysUser sysUser) {
        System.out.println(sysUser);


        // System.out.println("sysUser2 ok");


    }

    //当需要在jso页面中访问从controller上获取的信息
    @ModelAttribute("sysUser2")
    public SysUser getSysUser() {
        SysUser sysUser = new SysUser();
        sysUser.setId(1);
        sysUser.setJobNumber("KF001");
        return sysUser;
    }

    @GetMapping("/hello")
    public String hello() {
        return "hello";

    }

    @Resource
    SysUserService sysUserService = null;


    @GetMapping("/get/id/{id}")
    @ResponseBody
    public Map <String, Object> get(@PathVariable("id") Integer id) {
        SysUser sysUser = sysUserService.selectById(id);
        Map <String, Object> responsMap = new HashMap <>();
        responsMap.put("code", 0);
        responsMap.put("data", 123);
        responsMap.put("msg", "查询成功");
        return responsMap;

    }

    //@RequestMapping(value = "/login",method = RequestMethod.POST)
//    @RequestHeader 中的注解参数
//    name/value/作用相同,给参数命名
//    defaultVaule 默认值
//            required 是否必须(true表示不可为空)
    @RequestMapping(value = "/login")
    @ResponseBody
    public Map <String, Object> login(@RequestParam("jobNumber") String jobNumber, @RequestParam("password") String password) throws IOException {
        System.out.println("jobNumber:" + jobNumber);
        System.out.println("password:" + password);
        //如果登录验证不通过,抛出业务异常
//        if(1==1){
//            //throw new BusinessException(ErrorCode.LOGIN_FAILED);
//            throw new IOException("发生了未知异常");
//
//        }

        MapUtil mapUtil = new MapUtil();
        mapUtil.setCode("0");
        mapUtil.setMsg("登录成功");
        return mapUtil.getValues();

    }

    @RequestMapping(value = "/login2")
    @ResponseBody
    public Map <String, Object> login(@RequestBody SysUser sysUser) {
        System.out.println("sysUser.jobNumber" + sysUser.getJobNumber());
        System.out.println("sysUser.password" + sysUser.getPassword());

        MapUtil mapUtil = new MapUtil();
        mapUtil.setCode("0");
        mapUtil.setMsg("登录成功");
        return mapUtil.getValues();


    }

    @RequestMapping(value = "/login3")
    @ResponseBody
    public Map <String, Object> login(@RequestBody Map<String,Object> map) {
        System.out.println("map"+map);

        MapUtil mapUtil = new MapUtil();
        mapUtil.setCode("0");
        mapUtil.setMsg("登录成功");
        return mapUtil.getValues();


    }
}
